﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/TextBlockWrapper.cs
//  *
//  ***************************************************************************

#endregion

using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Windows;

using Haplous.Core;

using JetBrains.Annotations;

namespace Haplous.Presentation.ViewModel
{
    /// <summary>
    ///     A wrapper that allows text blocks to be used in the view.
    /// </summary>
    [DebuggerDisplay("TextBlockWrapper: Name={Block.Name}")]
    internal class TextBlockWrapper : BlockWrapper
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="BlockWrapper" /> class.
        /// </summary>
        /// <param name="viewModel">The view model to which this wrapper belongs.</param>
        /// <param name="block">The block being wrapped.</param>
        /// <param name="zOrder">The Z-order of this block.</param>
        public TextBlockWrapper([NotNull] GameAreaViewModel viewModel, [NotNull] ITextBlock block, int zOrder) : base(viewModel, block, zOrder)
        {
            block.Foreground.PropertyChanged += ForegroundChanged;
        }

        /// <summary>
        ///     Gets the foreground.
        /// </summary>
        /// <value>The foreground.</value>
        [NotNull]
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        [UsedImplicitly]
        public Color Foreground
        {
            get { return Block.Foreground; }
        }

        /// <summary>
        ///     The block being wrapped.
        /// </summary>
        /// <value>The block.</value>
        [NotNull]
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        [UsedImplicitly]
        public new ITextBlock Block
        {
            get { return (ITextBlock)base.Block; }
        }

        /// <summary>
        ///     The text to display.
        /// </summary>
        [CanBeNull]
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        [UsedImplicitly]
        public string Text
        {
            get { return Block.Text; }
        }

        /// <summary>
        ///     The font size, in pixels.
        /// </summary>
        /// <value>The size of the font.</value>
        /// <exception cref="System.ArgumentOutOfRangeException"><see cref="FontSize" /> must be at least 4 pixels.</exception>
        /// <remarks>Measured in pixels.  The minimum size permitted is 4 pixels.</remarks>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        [UsedImplicitly]
        public double FontSize
        {
            get
            {
                // TODO: Work out how to do text scaling in both X and Y axis.
                return Block.FontSize;
            }
        }

        /// <summary>
        ///     Gets the text alignment.
        /// </summary>
        /// <value>The text alignment.</value>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        [UsedImplicitly]
        public TextAlignment TextAlignment
        {
            get { return (TextAlignment)Block.HorizontalAlignment; }
        }

        /// <summary>
        ///     The font family to use when display the text.
        /// </summary>
        [NotNull]
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        [UsedImplicitly]
        public string FontFamily
        {
            get { return Block.FontFamily; }
        }

        /// <summary>
        ///     Called when a property on the foreground object changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="PropertyChangedEventArgs" /> instance containing the event data.</param>
        private void ForegroundChanged([NotNull] object sender, [NotNull] PropertyChangedEventArgs e)
        {
            switch(e.PropertyName)
            {
                case "Alpha":
                case "Red":
                case "Green":
                case "Blue":
                    Dispatcher.Invoke(() => OnPropertyChanged("Foreground"));
                    break;
            }
        }

        /// <summary>
        ///     Called when a property changes on the block and is not handled.
        /// </summary>
        /// <param name="propertyName">The name of the changed property.</param>
        protected override void OnBlockPropertyChanged(string propertyName)
        {
            switch(propertyName)
            {
                case "Text":
                case "FontSize":
                case "FontFamily":
                    Dispatcher.Invoke(() => OnPropertyChanged(propertyName));
                    break;
            }
        }
    }
}